/**
 * @project name:mail
 */
package com.situ.mail.shop.controller;

import com.situ.mail.shop.pojo.User;
import com.situ.mail.shop.service.IndexService;
import com.situ.mail.utils.ConfigUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author : yun&bo
 * @CreateDate : ==>2020/12/24 9:08
 * @ClassName : ==>
 * @Description : ==>
 * @Version
 */
@RestController
public class IndexController {


    private static final String SHOP_INDEX = "shop/index";
    private static final String SHOP_LOGIN = "shop/login";

    private IndexService indexService;

    public IndexController(IndexService indexService) {
        this.indexService = indexService;
    }


    /**
     * 从页面拿key key 正确key 登入
     * <p>
     * key不正确 拿session 有自动登录  没有抬走
     * <p>
     * session 也没有 跳转找登录界面
     *
     * @param modelAndView
     * @param req
     * @return
     */
    @RequestMapping({"/", "/index"})
    public ModelAndView goIndex(ModelAndView modelAndView, @CookieValue(value = ConfigUtil.COOKIE_NAME, required = false) String cookie, HttpServletRequest req) {
        if (cookie != null && "".equals(cookie)) {
            indexService.checkAutoLogin(cookie, req);
        }
        if (req.getSession().getAttribute(ConfigUtil.SESSION_LOGIN_USER_NAME) != null) {
            modelAndView.setViewName(SHOP_INDEX);
        } else {
            modelAndView.setViewName(SHOP_LOGIN);
        }
        return modelAndView;
    }

    @RequestMapping("/login")
    public ModelAndView goLogin(ModelAndView modelAndView) {
        modelAndView.setViewName(SHOP_LOGIN);
        return modelAndView;
    }


    /**
     * 执行登录
     */
    @PostMapping("/dologin")
    public Integer doLogin(String isRemember, User user, HttpServletRequest req, HttpServletResponse res) {
        Integer integer = indexService.doUserLogin(user, isRemember, req, res);
        return integer;
    }


    @RequestMapping("/test")
    public ModelAndView gg(ModelAndView modelAndView) {
        System.out.println("test!!!");
        modelAndView.setViewName("shop/test");
        return  modelAndView;
    }



}
